import {Component, OnInit, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core';
import {AuthService} from "@shared/service/auth/auth.service";
import {ActivatedRoute, Params, ParamMap, Router} from "@angular/router";

import {MineService} from "@mine/mine.service";
import {CommonPageVo, SNSResult, UserDeliveryAddressVO} from "@interface/index";



@Component({
  selector: 'seed-address-list',
  templateUrl: './address-list.component.html',
  styleUrls: ['./address-list.component.scss'],
  encapsulation: ViewEncapsulation.None,
})
export class AddressListComponent implements OnInit {
  private userId: string;
  public addressList: Array<UserDeliveryAddressVO>;
  public goodsQuantity = 0;
  public skuCode: string;
  public activeIndex: any = 0;
  public goodsCateCode1 = '';
  public haggleInfo = { // 砍价ID 和 砍价发起人ID
    haggleId: 0,
    initiatorId: '',
  };
  constructor(public mineService: MineService,
              public authService: AuthService,
              public router: Router,
              private route: ActivatedRoute
  ) {
    this.userId = this.authService.getUserId();
    if (localStorage.getItem("addressIndex")) {
      this.activeIndex = localStorage.getItem("addressIndex");
    }
    this.route.queryParams
      .subscribe((param: Params) => {
        this.skuCode = param['skuCode'];
        this.goodsQuantity = param['quantity'];
        this.goodsCateCode1 = param['goodsCateCode'];
        this.haggleInfo.haggleId = param['haggleId'];
        this.haggleInfo.initiatorId = param['initiatorId'];
      });
  }

  goBackOrderConfirm(i) {
    localStorage.setItem('addressIndex', i);
    this.router.navigate(['/order/confirm-order'], {
      queryParams: {
        haggleId: this.haggleInfo.haggleId,
        initiatorId: this.haggleInfo.initiatorId,
        skuCode: this.skuCode,
        quantity: this.goodsQuantity,
        goodsCateCode: this.goodsCateCode1
      }
    });
  }

  getAddressList() {
    this.mineService.getUserAddress(this.userId)
      .subscribe((res: SNSResult<CommonPageVo<UserDeliveryAddressVO>>) => {
        if (res.result === '0') {
          this.addressList = res.data.list;
        }
      });
  }

  addNewAddress() {
    this.router.navigate(['/mine/address/form'], {
      queryParams: {
        type: 'insert',
        haggleId: this.haggleInfo.haggleId,
        initiatorId: this.haggleInfo.initiatorId,
        skuCode: this.skuCode,
        quantity: this.goodsQuantity,
        goodsCateCode: this.goodsCateCode1
      }
    });

  }

  ngOnInit() {
    this.getAddressList();
  }

}
